Repository navigation
Conversation
The generated IR for some of the AST nodes handled by `MLIRScanner::VisitBinaryOperator()` and `MLIRScanner::VisitCastExpr()` depends on the sign of the operand or result types. Some of these checks attempt to directly cast the result of `Expr::getType()` to `BuiltinType` and then check if the resulting `QualType` is a signed or unsigned integer type. If the cast fails, the type is assumed to be signed. However, the type being checked may not directly be a builtin type (e.g., it may be an alias for an unsigned type wrapped in a `TypedefType`) and the default assumption may not hold. Examining the canonical type retrieved via `QualType::getCanonicalType()`, as implemented by this change, solves the issue.
|
Kindly asking if anyone is in for a review of this PR. Considering the contributions to the affected files, maybe @wsmoses? |
|
Thanks for this. I hit the same bug independently and can confirm the fix. There are four more sites with the same pattern in Minimal reproducer (plain C): typedef unsigned int u32;
long long widen(u32 x) { return x; } // arith.extsi, should be extui
typedef unsigned char u8;
int div3(u8 x) { return x / 3; } // arith.extsi i8 -> i32Written directly as Remaining sites (same fix, @@ -4122 (CK_IntegralToFloating)
- if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getSubExpr()->getType())) {
+ if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getSubExpr()->getType().getCanonicalType())) {
@@ -4141 (CK_FloatingToIntegral)
- if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType())) {
+ if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType().getCanonicalType())) {
@@ -4366 (CK_IntegralToBoolean)
- if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType())) {
+ if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType().getCanonicalType())) {
@@ -4386 (CK_FloatingToBoolean)
- if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType())) {
+ if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType().getCanonicalType())) {Verification: a differential test compared the same code compiled with clang and executed against the cgeist lowering run through
Happy to open a follow-up PR on top of this one if that's easier, or you're welcome to fold these four lines in. Generated with Claude Opus 5.5 |
The generated IR for some of the AST nodes handled by
MLIRScanner::VisitBinaryOperator()andMLIRScanner::VisitCastExpr()depends on the sign of the operand or result types. Some of these checks attempt to directly cast the result ofExpr::getType()toBuiltinTypeand then check if the resultingQualTypeis a signed or unsigned integer type. If the cast fails, the type is assumed to be signed.However, the type being checked may not directly be a builtin type (e.g., it may be an alias for an unsigned type wrapped in a
TypedefType) and the default assumption may not hold.Examining the canonical type retrieved via
QualType::getCanonicalType(), as implemented by this change, solves the issue.